package com.sharp.authorization.controller.admin;

import com.sharp.authorization.annotation.auth.AdminAuthRole;
import com.sharp.authorization.annotation.auth.Auth;
import com.sharp.authorization.service.permit.PermitCoreService;
import com.sharp.authorization.vo.permit.*;
import com.sharp.kernel.config.response.Response;
import com.sharp.kernel.config.response.ResponseHandler;
import com.sharp.kernel.vo.common.CommonResVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * Title: AdminPermitController
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/4/9 18:01
 * @description Project Name: Mall
 * @Package: com.srct.service.account.controller.admin
 */
@ApiIgnore
@Auth(AdminAuthRole.class)
@Api(value = "用户权限管理", tags = "后台管理员")
@RestController("AdminPermitController")
@RequestMapping(value = "/admin/permit")
public class AdminPermitController {

    @Resource
    private PermitCoreService permitCoreService;

    @ApiOperation(value = "获取全部权限树", notes = "系统管理员准入")
    @RequestMapping(value = "/allPermitTree", method = RequestMethod.GET)
    public ResponseEntity<Response<List<AllPermitTreeRes>>> getAllPermitTree(
            @RequestParam(value = "clientType", required = false) String clientType) {
        List<AllPermitTreeRes> res = permitCoreService.getAllPermitTree(clientType);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "获取角色权限列表", notes = "系统管理员准入")
    @RequestMapping(value = "/roleList", method = RequestMethod.GET)
    public ResponseEntity<Response<List<PermitRes>>> getRolePermitList(
            @RequestParam(value = "roleId", required = false) List<String> roleIdList) {
        List<PermitRes> res = permitCoreService.getRolePermitList(roleIdList);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "获取用户权限列表", notes = "系统管理员准入")
    @RequestMapping(value = "/userPermitTree", method = RequestMethod.GET)
    public ResponseEntity<Response<List<PermitTreeRes>>> getUserPermitTree(String userId, String clientType) {
        List<PermitTreeRes> res = permitCoreService.getUserPermitTree(userId, clientType);
        return ResponseHandler.generateResponse(res);
    }

    @ApiOperation(value = "添加菜单", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResponseEntity<Response<CommonResVO>> addRole(@RequestBody AddPermitReq req) {
        permitCoreService.addPermit(req);
        return ResponseHandler.commonResponse("添加菜单成功");
    }

    @ApiOperation(value = "修改菜单", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<Response<CommonResVO>> updateRole(@RequestBody UpdatePermitReq req) {
        permitCoreService.updatePermit(req);
        return ResponseHandler.commonResponse("修改菜单成功");
    }

    @ApiOperation(value = "删除菜单", notes = "系统管理员准入")
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResponseEntity<Response<CommonResVO>> deleteRole(@RequestBody DeletePermitReq req) {
        permitCoreService.deletePermit(req);
        return ResponseHandler.commonResponse("删除菜单成功");
    }
}
